// pages/buyservice/buyservice.js
const util = require('../../utils/util.js');
const api = require('../../config/api.js');
const wxpay = require('../../services/pay.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    servicedata:'',
    userinfo:{
      cont: 1,
      user_id: 0,
      serviceinfo: '',
      
    }
  
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;

    let userids = wx.getStorageSync('userids');
    let reserve = that.data.userinfo;
    reserve.user_id = userids;

    util.request(api.Reserve, {
      'cont': '1'
    }, 'POST').then(function (res) {
      console.log(res);
      if (res.code == 0) {
      

        that.setData({
          servicedata: res.data.service,
       
          areadata: res.data.ragion,

          userinfo: reserve,

        })

      } else {
        wx: wx.showToast({
          title: '数据初始化失败请检查网络并刷新当前页面',
          icon: 'none',
          image: '',
          duration: 2000
        })
      }
    });
   
  
  },
  /**
 * 提交订单
 */
  saveOrder: function (e) {
    let that = this;
    let reserve = that.data.userinfo;
    console.log(reserve);
    if (that.validate()) {
      var servicedata_arr = that.data.servicedata;
      var sercivearr = [];
      for (var i = 0; i < servicedata_arr.length; i++) {
        if (servicedata_arr[i].number != 0) {
          sercivearr.splice(1, 0, servicedata_arr[i])
        }
      }
      reserve.serviceinfo = JSON.stringify(sercivearr);
      util.request(api.Order, reserve, 'POST').then(function (res) {
        console.log(res);
        if (res.code == 0) {
          wxpay.wxpay(res.data.totalfee, res.data.tradeno, '/pages/countingcard/countingcard', that.data.userinfo.user_id);

          wx.showToast({
            title: '提交成功',
            icon: 'success',
            duration: 2000,
          })

          // wx.redirectTo({
          //   url: '../orderdetails/orderdetails?orderId=' + res.data.orderid
          // })
          // wx.navigateBack();

          that.setData({


          })

        }
      });
    }

  },
  
  validate() {
    let that = this;
    let reserve = that.data.userinfo;
    let serinfo = that.data.servicedata;
    let sernum = 0;
    for (let i = 0; i < serinfo.length; i++) {
      sernum += serinfo[i].number;
    }
    console.log(sernum);
    if (sernum == 0) {
      util.showErrorToast("未选择服务");
      return false;
    }
    return true;

  },
  // 服务项目数量选择
  addNumber: function (e) {
    var index = e.currentTarget.dataset.index;
    // console.log(index);
    let that = this;
    let service = that.data.servicedata;
    if (service[index].isncard) {
      wx.showToast({
        title: '已使用计次卡',
        icon: 'none',
        duration: 2000,
      })
      return;

    }
    service[index].number++;
    that.setData({
      servicedata: service,
    })
  },

  cutNumber: function (e) {
    var index = e.currentTarget.dataset.index;
    let that = this;
    let service = that.data.servicedata;
    if (service[index].isncard) {
      wx.showToast({
        title: '已使用计次卡',
        icon: 'none',
        duration: 2000,
      })
      return;

    }
    if (service[index].number <= 0) {
      util.showErrorToast("我也是有底线的");
      service[index].number = 0;
    } else {
      service[index].number--;
    }

    that.setData({
      servicedata: service,
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})